#include "GraphicConfig.h"
#include <stdio.h>


GxSDK::CGraphicConfig::CGraphicConfig(void)
	:FullScreen(false)
	,MultiSample(false)
	,VSync(false)
	,Anisotropy(false)
	,MultiSampleCount(0)
	,AnisotropyCount(0)
	,ResolutionX(1024)
	,ResolutionY(768)
{
}


GxSDK::CGraphicConfig::~CGraphicConfig(void)
{
}

int GxSDK::CGraphicConfig::LoadCfg( const WCHAR* filename /*= L"GraphicSetting.cfg"*/, bool fullPath /*=false*/ )
{
	static WCHAR buf[256] = {0};
	static WCHAR fullName[256] = {0};
	if (!fullPath)
	{
		GetModuleFileName(NULL, buf, 256);
		wsprintf(fullName, L"%s\\%s", GetFullPath(buf), filename);
	}
	else
	{
		wcscpy(fullName, filename);
	}

	if(
		GetBoolFromIni(L"GraphicSetting", L"bFullScreen", FullScreen, fullName) && 
		GetBoolFromIni(L"GraphicSetting", L"bMultiSample", MultiSample, fullName) &&
		GetIntFromIni(L"GraphicSetting", L"MultiSampleCount", MultiSampleCount, fullName) &&
		GetBoolFromIni(L"GraphicSetting", L"bAnisotropy", Anisotropy, fullName) &&
		GetIntFromIni(L"GraphicSetting", L"AnisotropyCount", AnisotropyCount, fullName) &&
		GetBoolFromIni(L"GraphicSetting", L"bVSync", VSync, fullName) &&
		GetIntFromIni(L"GraphicSetting", L"ResolutionX", ResolutionX, fullName) &&
		GetIntFromIni(L"GraphicSetting", L"ResolutionY", ResolutionY, fullName)
		)
		return true;

	return false;
}

int GxSDK::CGraphicConfig::SaveToFile( const WCHAR* filename /*= L"GraphicSetting.cfg"*/, bool fullPath /*=false*/ )
{
	static WCHAR buf[256] = {0};
	static WCHAR fullName[256] = {0};
	if (!fullPath)
	{
		GetModuleFileName(NULL, buf, 256);
		wsprintf(fullName, L"%s\\%s", GetFullPath(buf), filename);
	}
	else
	{
		wcscpy(fullName, filename);
	}
	
	SetBoolToIni(L"GraphicSetting", L"bMultiSample", MultiSample, fullName);
	SetIntToIni(L"GraphicSetting", L"MultiSampleCount", MultiSampleCount, fullName);
	SetBoolToIni(L"GraphicSetting", L"bAnisotropy", Anisotropy, fullName);
	SetIntToIni(L"GraphicSetting", L"AnisotropyCount", AnisotropyCount, fullName);
	SetBoolToIni(L"GraphicSetting", L"bFullScreen", FullScreen, fullName);
	SetBoolToIni(L"GraphicSetting", L"bVSync", VSync, fullName);
	SetIntToIni(L"GraphicSetting", L"ResolutionX", ResolutionX, fullName);
	SetIntToIni(L"GraphicSetting", L"ResolutionY", ResolutionY, fullName);

	return true;
}

bool GxSDK::CGraphicConfig::GetBoolFromIni( const WCHAR* section, const WCHAR* key, bool &value, const WCHAR* filename )
{
	static WCHAR buf[256] = {0};
	GetPrivateProfileString(section, key, NULL, buf, 256, filename);
	
	if (!wcscmp(L"TRUE", _wcsupr(buf)))
	{
		value = true;
		return true;
	}
	else if (!wcscmp(L"FALSE", _wcsupr(buf)))
	{
		value = false;
		return true;
	}
	else
	{
		value = false;
		return false;
	}
}

bool GxSDK::CGraphicConfig::GetIntFromIni( const WCHAR* section, const WCHAR* key, int &value, const WCHAR* filename )
{
	static WCHAR buf[256] = {0};
	static const WCHAR defalutValue[32] = L"-65535";
	GetPrivateProfileString(section, key, defalutValue, buf, 256, filename);
	
	if (wcscmp(defalutValue, buf))
	{
		value = _wtoi(buf);
		return true;
	}
	else
	{
		value = 0;
		return false;
	}
}

bool GxSDK::CGraphicConfig::GetFloatFromIni( const WCHAR* section, const WCHAR* key, float &value, const WCHAR* filename )
{
	static WCHAR buf[256] = {0};
	static const WCHAR defalutValue[32] = L"-65535.0";
	GetPrivateProfileString(section, key, defalutValue, buf, 256, filename);

	if (wcscmp(defalutValue, buf))
	{
		value = static_cast<float>(_wtof(buf));
		return true;
	}
	else
	{
		value = 0.0f;
		return false;
	}
}

const WCHAR* GxSDK::CGraphicConfig::GetFullPath( const WCHAR* fullfilepath )
{
	static WCHAR buf[256] = {0};
	wcscpy(buf, fullfilepath);
	int nLen = wcslen(buf);
	while (--nLen)
	{
		if (buf[nLen] == '\\' || buf[nLen] == '/')
		{
			buf[nLen] = '\0';
			break;
		}
	}

	return buf;
}

void GxSDK::CGraphicConfig::SetBoolToIni( const WCHAR* section, const WCHAR* key, bool &value, const WCHAR* filename )
{
	if (value)
	{
		WritePrivateProfileString(section, key, L"TRUE", filename);
	}
	else
	{
		WritePrivateProfileString(section, key, L"FALSE", filename);
	}
}

void GxSDK::CGraphicConfig::SetIntToIni( const WCHAR* section, const WCHAR* key, int &value, const WCHAR* filename )
{
	static WCHAR buf[32] = {0};
	_itow(value, buf, 10);
	WritePrivateProfileString(section, key, buf, filename);
}

void GxSDK::CGraphicConfig::SetFloatToIni( const WCHAR* section, const WCHAR* key, float &value, const WCHAR* filename )
{
	static WCHAR buf[32] = {0};
	swprintf(buf, L"%4f", value);
	//wsprintf(buf, L"%.4f", value);
	WritePrivateProfileString(section, key, buf, filename);
}
